package com.qianguo.sender.usercenter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.entity.ResultData;
import com.eva.androidlibrary.widget.mdedittext.MaterialEditText;
import com.qianguo.sender.R;
import com.qianguo.sender.base.BaseTitleActivity;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * 修改密码页面
 *
 * @author GUXIA318
 * @date 2015-09-25 10:22
 */
@EActivity(R.layout.activity_sender_modify_password)
public class SenderModifyPasswordActivity extends BaseTitleActivity {

    public static void jumpToModifyPassword(Context context) {
        context.startActivity(new Intent(context, SenderModifyPasswordActivity_.class));
    }

    @ViewById(R.id.sender_modify_password_old_edit_text)
    MaterialEditText mEdtOld;

    @ViewById(R.id.sender_modify_password_new_edit_text)
    MaterialEditText mEdtNew;

    @ViewById(R.id.sender_modify_password_confirm_edit_text)
    MaterialEditText mEdtConfirm;

    @Click(R.id.sender_modify_password_ok_button)
    void clickOk() {
        String oldPsw = mEdtOld.getText().toString();
        String newPsw = mEdtNew.getText().toString();
        String confirmPsw = mEdtConfirm.getText().toString();
        if (validateInfo(oldPsw, newPsw, confirmPsw)) {
            modifyPassword(oldPsw, newPsw);
        }
    }

    @Background
    void modifyPassword(String oldPsw, String newPsw) {
        showLoadingDialog(this);
        mApp.getApi().modifyPwd(mApp.getUser().getToken(), oldPsw, newPsw, new HttpRequestCallback<ResultData>() {
            @Override
            public void onSuccess(int code, String msg, ResultData data) {
                closeLoadingDialog();
                // 修改密码成功，重新登陆
                showDialog(SenderModifyPasswordActivity.this, getString(R.string.hint), getString(R.string.sender_modify_password_success)
                        , new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                closeDialog();
                            }
                        }
                );
            }

            @Override
            public void onFailure(int code, String msg) {
                closeLoadingDialog();
                showSnackBar(SenderModifyPasswordActivity.this, msg);
            }
        });

    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.sender_modify_password);
    }

    @Override
    protected void initializeUI() {

    }

    private boolean validateInfo(String oldPsw, String newPsw, String confirmPsw) {
        if (oldPsw.length() < 6 || oldPsw.length() > 20) {
            mEdtOld.setError(getString(R.string.edit_error_password));
            return false;
        }

        if (newPsw.length() < 6 || newPsw.length() > 20) {
            mEdtNew.setError(getString(R.string.edit_error_password));
            return false;
        }

        if (!newPsw.equals(confirmPsw)) {
            mEdtConfirm.setError(getString(R.string.sender_modify_password_error));
            return false;
        }

        return true;
    }
}
